/**
 * 新增偏激品牌
 */
import { ApiRequest } from '@api/basePage';
import { BRAND_SOURCE_OPTIONS } from '@/constants/commodity'
export default {
  data () {
    return {
      brandSourceOptions: BRAND_SOURCE_OPTIONS,
      sLoading: false,
      uuid: '',
      brandImageKey1: [], // 正方形图标回显地址
      brandImageKey2: [], // 长方形图标回显地址
      brandImageKey3: [], // 厂家Logo
      brandFormData: {
        brandName: '',
        brandEnName: '',
        note: '',
        imageKey1: '',
        imageKey2: '',
        factoryLogo: '',
        insideLevel: '',
        outsideLevel: '',
        series: '', // 系列
        parentUuid: '',
        brandState: '1',
        factoryInfluenceMore: '2', // 厂家品牌影响力是否大于商品品牌 默认为 否
        manufacturerSimpleName: '', // 生产厂家简称
        manufacturerName: '', // 生产厂家全称
        brandSource: '', // 品牌来源
        sourceDetail: '', // 来源详情
        showOnline: 1, // 是否线上展示 1是、0否
        brandRegisterNo: '', // 商标注册号
        productNameType: '1', // 商品名称是否包含品牌
      },
      resetBrandFormData: {},
      brandFormRules: {
        hasBarCode: [
          {
            required: true,
            message: '请选择是否有商品条形码',
            trigger: 'change'
          }
        ],
        brandName: [
          {
            required: true,
            message: '请填写品牌名称',
            trigger: 'change'
          },
          {
            type: 'string',
            min: 1,
            message: '品牌名称不得少于一个字符',
            trigger: 'blur'
          }
        ],
        note: [
          {
            required: true,
            message: '请填写品牌描述',
            trigger: 'change'
          },
          {
            type: 'string',
            min: 5,
            message: '品牌描述不得少于5个字符',
            trigger: 'blur'
          }
        ],
        brandSource: [
          {
            required: true,
            message: '请选择品牌来源',
            trigger: 'change'
          }
        ],
        sourceDetail: [
          {
            required: true,
            message: '请填写来源详情',
            trigger: 'blur'
          }
        ],
        brandRegisterNo: [
          {
            required: true,
            message: '请填写商标注册号',
            trigger: 'blur'
          }
        ],
        imageKey2: [
          {
            required: true,
            message: '请上传商标注册证',
            trigger: 'change'
          }
        ],
        imageKey1: [
          {
            required: true,
            message: '请上传品牌logo',
            trigger: 'change'
          }
        ],
        factoryBrandName: [
          {
            required: true,
            message: '请填写厂家品牌名称',
            trigger: 'blur'
          }
        ],
      },
    }
  },
  created () {
    this.uuid = this.$route.query.uuid;
    this.resetBrandFormData = JSON.parse(JSON.stringify(this.brandFormData));
  },
  methods: {
    // 上传正方形图片 品牌logo
    changeImageKey1 (imgArr) {
      let bool = true
      this.brandImageKey1 = imgArr
      imgArr.forEach((item, index) => {
        if (index === 0) {
          this.brandFormData.imageKey1 = item.response.retData;
          // 上传图片时，不进行置空
          bool = false
        }
      })
      if (bool) {
        // 删除图片时置空
        this.brandFormData.imageKey1 = '';
      }
    },
    // 上传长方形图片 商标注册证
    changeImageKey2 (imgArr) {
      let bool = true
      this.brandImageKey2 = imgArr
      imgArr.forEach((item, index) => {
        if (index === 0) {
          // 上传图片时，不进行置空
          bool = false
          this.brandFormData.imageKey2 = item.response.retData;
        }
      })
      if (bool) {
        // 删除图片时置空
        this.brandFormData.imageKey2 = '';
      }
    },
    // 上传厂家logo 厂家全称logo
    changeImageKey3 (imgArr) {
      let bool = true
      this.brandImageKey3 = imgArr
      imgArr.forEach((item, index) => {
        if (index === 0) {
          // 上传图片时，不进行置空
          bool = false
          this.brandFormData.factoryLogo = item.response.retData;
        }
      })
      if (bool) {
        // 删除图片时置空
        this.brandFormData.factoryLogo = '';
      }
    },
    // 提交
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        let msgTxt = '新增';
        if (valid) {
          if (this.sLoading) {
            return false;
          }
          this.sLoading = true;
          let data = {
            uuid: this.uuid,
            ...this.brandFormData
          }
          if (this.uuid) {
            msgTxt = '更新'
          }
          // 开始请求会员信息
          ApiRequest(this.requestApi.saveBrandInfo.url, this.requestApi.saveBrandInfo.type, data).then(res => {
            if (res.retStatus === '1') {
              this.$Message.success('品牌' + msgTxt + '成功!');
              this.$router.go(-1)
            }
            this.sLoading = false;
          })
        } else {
          this.$Message.error('品牌' + msgTxt + '失败，请稍后重试!');
        }
      })
    },
    // 清空
    handleReset (name) {
      this.brandFormData = JSON.parse(JSON.stringify(this.resetBrandFormData));
    }
  }
}
